package io.xxx.xbutler.core.callback;

import com.alibaba.fastjson2.JSONObject;
import io.xxx.xbutler.data.TagMapper;
import io.xxx.xbutler.domain.Callback;
import io.xxx.xbutler.domain.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 新建标签回调
 *
 * <pre>
 * {
 *     "nType": 2002,
 *     "vcMerchantNo": "",
 *     "vcRobotWxId": "",
 *     "vcRobotSerialNo": "",
 *     "vcSerialNo": "",
 *     "nResult": 1,
 *     "vcResult": "SUCCESS",
 *     "Data": {
 *         "nTagId": 1,
 *         "vcTagName": "",
 *         "vcBase64TagName": ""
 *     }
 * }
 * </pre>
 */
@Slf4j
@Component
public class _2002Handler implements CallbackHandler {

    @Resource
    private TagMapper tagMapper;

    @Override
    public String name() {
        return "新建标签回调";
    }

    @Override
    public boolean supports(int type) {
        return type == 2002;
    }

    @Override
    public void handle(Callback callback) {
        JSONObject data = (JSONObject) callback.getData();
        Tag tag = new Tag()
                .setId(data.getLong("nTagId"))
                .setName(data.getString("vcTagName"))
                .setBase64Name(data.getString("vcBase64TagName"))
                .setContactId(callback.getRobotId())
                .setContactSerialNo(callback.getRobotSerialNo())
                .setCreatedTime(LocalDateTime.now());
        tagMapper.insert(tag);
    }
}
